package com.mine.interview;

import org.junit.Test;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 控制线程的顺序
 */
public class ControlThread {
    @Test
    public void join() throws InterruptedException {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.MILLISECONDS.sleep(new Random().nextInt(100));
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        Thread thread3 = new Thread(runnable);
        thread1.start();
        thread1.wait();

        thread2.start();
        thread2.wait();

        thread3.start();
        thread3.join();

        TimeUnit.SECONDS.sleep(1);
    }
}
